package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分类管理的分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类管理的分页查询")
    public Result<PageResult> categoryPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("需要分页的大小和页数: {}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.categoryPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> queryByType(String type){
        log.info("需要查询的的菜品类型为：{}",type);
        List<Category> category = categoryService.queryByType(type);
        return Result.success(category);

    }

    /**
     * 修改分类管理
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类管理")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }
    /**
     * 启用或禁用分类管理
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用分类管理")
    public Result enableOrDisable(@PathVariable("status") Integer status,Long id){
        categoryService.enableOrDisable(status,id);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteCategoryById(Long id){
        categoryService.deleteCategoryById(id);
        return Result.success();
    }
}

